package org.qydjk.console.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.AlipayUserinfo;
import org.qydjk.common.pojo.WechatUserinfo;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.console.vo.AlipayUserinfoBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lanan on 2018/1/10 0010.
 */
@Controller
@RequestMapping("/user/aliPayCustomer")
public class AliPayCustomerController extends BaseController {
    /**
     * 视图
     * author: Lanan 2018-01-10
     */
    @RequestMapping("/index.do")
    public String weChatCustomer() {
        return "user/alipay/aliPayCustomer";
    }

    @RequestMapping("/list.do")
    @ResponseBody
    public Object aliPayCustomerList(String searchInfo, int page, int rows) {
        List<AlipayUserinfo> alipayUserinfos = new ArrayList<AlipayUserinfo>();
        String sql = "SELECT au.*, u.username as user_name, u.phone as user_phone, u.email as user_email FROM qy_alipay_userinfo au LEFT JOIN qy_user u ON au.alipay_user_id = u.alipay_user_id WHERE 1 = 1";
        String countSql = "SELECT count(au.id) FROM qy_alipay_userinfo au LEFT JOIN qy_user u ON au.alipay_user_id = u.alipay_user_id WHERE 1 = 1";
        if (!StringUtil.isEmptyOrNull(searchInfo)) {
            String whereStr = " AND (u.phone like '%"+ searchInfo +"%'" +
                    " OR u.email like '%" + searchInfo + "%'" +
                    " OR u.username like '%"+ searchInfo +"%'" +
                    " OR au.nick_name like '%"+ searchInfo +"%')";
            sql += whereStr;
            countSql += whereStr;
        }
        sql += " order by au.update_time desc";
        try {
            alipayUserinfos = baseService.findBeanBySQL(sql, AlipayUserinfoBean.class, new HibernatePage(page, rows));
            int totalCount = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(totalCount, alipayUserinfos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse(0, alipayUserinfos);
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public Object deleteAliPayCustomer(AlipayUserinfo alipayUserinfo) {
        JSONObject resp=new JSONObject();
        Integer id = alipayUserinfo.getId();
        if(id == null || id == 0){
            resp.put("success", false);
            resp.put("msg", "id不能为空！");
            return JSON.toJSON(resp);
        }
        try {
            String updateSql = "UPDATE qy_user SET alipay_user_id = '' WHERE alipay_user_id = '" + alipayUserinfo.getAlipayUserId() + "'";
            baseService.excuteSQL(updateSql);
            baseService.delete(alipayUserinfo);
            resp.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            resp.put("success", false);
            resp.put("msg", e.toString());
        }
        return JSON.toJSON(resp);
    }
}
